import Vue from 'vue';
import Vuex from 'vuex';
import * as actions from './actions';
import * as getters from './getters';

Vue.use(Vuex)

// 应用初始状态
const state = {
    Token: '',
    count: 0
}

// 定义所需的 mutations
const mutations = {
    INCREMENT(state) {
        state.count++;
    },
    DECREMENT(state) {
        state.count--;
    },
    SETTOKEN(state, payload) {
        state.Token = payload.token;
    }
};

//刷新保留数据
const debug = process.env.NODE_ENV !== 'production';

const storeInSessionstorage = store => {
    // 当 store 初始化后调用
    if (sessionStorage.getItem('state')) {
        store.replaceState(JSON.parse(sessionStorage.getItem('state')));
    }
    store.subscribe((mutation, state) => {
        sessionStorage.setItem('state', JSON.stringify(state));
    });
};

// 创建 store 实例
export default new Vuex.Store({
    actions,
    getters,
    strict: debug,
    plugins: [storeInSessionstorage],
    state,
    mutations
});